////////////////////////////////////////////////////////////////////////////////
//
//  ADOBE SYSTEMS INCORPORATED
//  Copyright 2010 Adobe Systems Incorporated
//  All Rights Reserved.
//
//  NOTICE: Adobe permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

package fr.axones.pocketagile.ui.components.tabbedviewnavigator
{
	import flash.display.DisplayObject;
	import flash.utils.ByteArray;
	
	import fr.axones.pocketagile.ui.components.tabbedviewnavigator.assets.Blank;
	import fr.axones.pocketagile.ui.components.viewnavigator.ViewNavigatorMultiIcon;
	
	import mx.core.DPIClassification;
	import mx.core.mx_internal;
	import mx.utils.DisplayUtil;
	
	import spark.components.Group;
	import spark.components.ResizeMode;
	import spark.primitives.BitmapImage;
	import spark.skins.mobile.supportClasses.ButtonBarButtonSkinBase;
	
	use namespace mx_internal;
	
	/**
	 *  ButtonBarButton skin base class for TabbedViewNavigator ButtonBarButtons.
	 * 
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 2.5 
	 *  @productversion Flex 4.5
	 */
	public class TabbedViewNavigatorTabBarTabSkin extends ButtonBarButtonSkinBase
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		public var iconSelect:Group;
		public var iconUnselect:Group;
		
		/**
		 *  Constructor.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 2.5
		 *  @productversion Flex 4.5
		 */
		public function TabbedViewNavigatorTabBarTabSkin()
		{
			super();
			
			useCenterAlignment = false;
			
			upBorderSkin = fr.axones.pocketagile.ui.components.tabbedviewnavigator.assets.Blank;
			downBorderSkin = fr.axones.pocketagile.ui.components.tabbedviewnavigator.assets.Blank;
			
			layoutBorderSize = 0;
			layoutPaddingLeft = 0;
			layoutPaddingRight = 0;
			layoutGap = 0;
		}
		
		override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.layoutContents(unscaledWidth, unscaledHeight);
			
			var labelHeight:Number = labelDisplay.getLayoutBoundsHeight();
			var labelY:Number = unscaledHeight - layoutPaddingBottom - labelHeight;
			
			setElementPosition(labelDisplay, labelDisplay.getLayoutBoundsX(), labelY);
			setElementPosition(labelDisplayShadow, labelDisplay.getLayoutBoundsX(), labelY);
			
			var isSelected:Boolean = currentState.indexOf("Selected") >= 0;
			
			if (isSelected)
			{
				labelDisplay.textColor = 0xFFFFFF;
				setIcon(hostComponent["data"]["iconSelected"]);
			}
			else
			{
				labelDisplay.textColor = getStyle("chromeColor");
				setIcon(hostComponent["data"]["iconUnselected"]);
			}
		}
		
		/**
		 *  @private
		 */
		override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
		{			
			graphics.beginFill(0, 0);
			graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			graphics.endFill();
		}
		
		/**
		 *  Sets the current icon for the iconDisplay skin part.
		 *  The iconDisplay skin part is created/set-up on demand.
		 *
		 * @param icon The current icon. 
		 * 
		 *  @see #getIconDisplay
		 *  @see #useIconStyle
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 2.5
		 *  @productversion Flex 4.5
		 */
		override protected function setIcon(icon:Object):void
		{		
			if(iconSelect && iconUnselect)
			{
				if((iconSelect.getElementAt(0) as BitmapImage).source == icon)
				{
					show(iconSelect);
					hide(iconUnselect);
				}
				else
				{
					show(iconUnselect);
					hide(iconSelect);
				}
				return;
			}
			
			iconSelect = addIcon(hostComponent["data"]["iconSelected"],0);
			iconUnselect = addIcon(hostComponent["data"]["iconUnselected"],1);
			
			invalidateSize();
			invalidateDisplayList();
		}
		
		private function show(iconHolder:Group):void
		{
			iconHolder.visible = true;
			iconHolder.includeInLayout = true;
		}
		
		private function hide(iconHolder:Group):void
		{
			iconHolder.visible = false;
			iconHolder.includeInLayout = false;			
		}
		
		private function addIcon(icon:Object, pos:int):Group
		{
			var iconHolder:Group;
			iconHolder = new Group();
			iconHolder.resizeMode = ResizeMode.SCALE;
			addChildAt(iconHolder,pos);
			
			var iconInstance:BitmapImage;
			iconInstance = new BitmapImage();
			iconInstance.source = icon;
			iconHolder.addElementAt(iconInstance as BitmapImage, 0);
			
			hide(iconHolder);
			
			return iconHolder;
		}
		
		/**
		 *  The current skin part that displays the icon.
		 *  If the icon is a Class, then the iconDisplay is an instance of that class.
		 *  If the icon is a DisplayObject instance, then the iconDisplay is that instance.
		 *  If the icon is URL, then iconDisplay is the Group that holds the BitmapImage with that URL.
		 *
		 *  @see #setIcon
		 *  @see #useIconStyle
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 2.5
		 *  @productversion Flex 4.5
		 */
		override protected function getIconDisplay():DisplayObject
		{
			var isSelected:Boolean = currentState.indexOf("Selected") >= 0;
			
			if(isSelected)
			{
				return iconSelect;
			}
			else
			{
				return iconUnselect;	
			}
			return iconSelect;
		}
	}
}